<?php

/*
 * WE ARE CHANGING THE PATH SINCE WE WANT TO BE ABLE TO INCLUDE THE FILES WITH
 * THEIR NORMAL PATHS ******** linuxCurrentPath:
 * /var/www/vhosts/1/123844/webspace/httpdocs/kekagkam.ondsl.gr/connection/functionality
 * * windowsCurrentPath: C:\xampp\htdocs\connection\courses/functionality *
 */
// error_reporting(E_ALL);
// ini_set('display_errors', 'On');
$WIN = 'C:\xampp\htdocs\connection\functionality';
$SERVER = '/var/www/vhosts/1/123844/webspace/httpdocs/kek-agkam.gr/functionality';
$UBUNTU = '/var/www/connection/functionality';
$_OS_path = $SERVER;

// echo $_OS_path. '<br />';
#echo getcwd() . '<br />';
// echo get_include_path().'<br />';

//chdir(__DIR__);

function Navigation ()
{
    $activePage = basename($_SERVER['PHP_SELF']);
    $rows = file(__DIR__ . '/navigation.txt');

    echo '<ul>';
    $html_menu = array();
    $count = 0;
    $found = false;

    foreach ($rows as $row)
    {
        $nav = explode("=", $row);
        $page = trim($nav[0]);
        $link = trim($nav[1]);

        if ($link == $activePage)
        {
            $found = true;
            $html_menu[] = '<li> <a class="current" href="#" >' . $page .
                     '</a></li>';
        }
        else
        {
            $html_menu[] = '<li><a href="' . $link . '" >' . $page . '</a></li>';
        }

        $count ++;
    } /* Used to HighLight the appropriate menu */

    if (! $found)
    {
        // Get the folders that exist in the file
        $frows = file(__DIR__ . '/folders.txt');
        // Find the current folder that the user is in
        $folder = find_folder($_SERVER['PHP_SELF'], $activePage);
        $folder = $folder == "selfunded" || $folder == "funded" ? "courses" : $folder;

        // Loop the folders
        for ($i = 0; $i < count($html_menu); $i ++)
        {
            // If you found the folder - (The index needs to be equal for
            // navigation and folder inputs)
            // cho trim ( $frows [$i] ).' == '.$folder.'</ br>';
            if (trim($frows[$i]) == $folder)
            {
                // Find the position in the string to add the class attribute
                $pos_to_add = strpos($html_menu[$i], "<a", 0);
                // Add the attribute ~class="current"~ to the menu item
                $html_menu[$i] = substr_replace($html_menu[$i],
                        ' class="current"', $pos_to_add + 2, 0);
                $found = true;
            }
        }
        /*
         * # If No menu folder BUT special folder if(! $found && $folder ==
         * "selfunded" || $folder == "funded") { # Find the position in the
         * string to add the class attribute $pos_to_add =
         * strpos($html_menu[2], "<a", 0); # Add the attribute
         * ~class="current"~ to the menu item $html_menu[2] =
         * substr_replace($html_menu[2], ' class="current"', $pos_to_add + 2,
         * 0); }
         */
    }
    for ($i = 0; $i < $count; $i ++)
    {
        print($html_menu[$i]);
    }

    echo '</ul>';
}

function header_links ()
{
    $page = 'Αρχική';
    $link = 'index.php';
    $base = basename($_SERVER['PHP_SELF']);
    if ($link == $base &&
             substr_count($_SERVER['PHP_SELF'], "courses", 0) <= 0)
    {
        $extra_links = <<<'LINKS'

LINKS;
        echo html_entity_decode($extra_links);
    }
    else
        return;
}

function find_folder ($path, $base)
{
    $pathSize = strlen($path);
    $dir = explode("/", $path);

    return trim($dir[count($dir)-2]);
}

?>
